package 查找;

import java.util.PriorityQueue;
import java.util.concurrent.*;

/**
 * @Author: biubiu0002
 * @Date: 2021-03-26 20:15
 **/
public class 二分查找 {

    int binarySearchDigui(int[] nums, int key, int low, int high) {
        int mid = low + ((high - low) >> 1);
        new ThreadPoolExecutor(1,4,4, TimeUnit.SECONDS,
                new ArrayBlockingQueue(10),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
                );


        if (low > high)
            return -1;
        if (nums[mid] == key)
            return mid;

        else if (nums[mid] > key)
            return binarySearchDigui(nums, key, low, mid - 1);
        else if (nums[mid] < key)
            return binarySearchDigui(nums, key, mid + 1, high);
        return -1;


    }


    int binarySearchWhile(int[] nums, int key, int low, int high) {

        while (low <= high) {
            int mid = low + ((high - low) >> 1);
//            int mid =low+(high-low)>>1; 错误！！！
//            int mid =(high+low)/2;
            if (nums[mid] == key) {
                return mid;
            }
            if (nums[mid] > key) {
                high = mid - 1;
            } else if (nums[mid] < key) {
                low = mid + 1;
            }
        }
        return -1;


    }

    public static void main(String[] args) {
        int nums[] = {1, 2, 3, 4, 5, 6, 7, 8};
//        int nums[] = {1,2};
        int index = new 二分查找().binarySearchWhile(nums, 6, 0, nums.length - 1);
//        int index2 = new 二分查找().binarySearchDigui(nums,2,0,nums.length-1);
        System.out.println(index);
//        System.out.println(index2);


    }


}
